﻿    window.setInterval(yourfunction, 10000);

    var numberOfColumns = 3;
    var numberOfColumnsInWebsitesGrid = 8;
    var maringsInPixels = 20;
    var webSitesLogosGapInPixels = 20;

    var currentGridBoxSize = 0;

    function yourfunction() { $(window).resize(); }

    function sendAjaxRequestTagModification(url, feedid) {
        var jqxhr = $.ajax(url)
        .fail(function (msg) { alert("Error...." + msg); })
        .done(function (msg) {
            //alert("Success..." + msg);
            $(".tagBox-" + feedid).remove();

            var output = "";
            var parts = msg.split(',');

            $.each(parts,
        function (key, part) {
            //output += '<span>' + part + '</span>';
            output += "<span class=\"tagBox-" + feedid + "\" style='border-radius:5px; margin-right:5px;margin-bottom:3px;border:1px solid lightgray;color:Gray;padding:2px;'><span><a href='/category/" + part + "'>" + part + " </a> <a style='color:red;margin-left:1px;' href=\"javascript:sendAjaxRequestTagModification('/websiteinfo/removetagfromfeed?feedid=" + feedid + "&tag=" + part + "'," + feedid + ")\">&times;</a></span></span>";
        });

            $("#tagBox-" + feedid).html(output);
        });
    }

    var isImageSizeInitialized = false;


    function ResizeAllMainContainersAccordingToWindowSize() {

        var currentWindowWidth = $(window).width();

        if (columnMode == "0" || columnMode == "1" || columnMode == "2") {

            if (columnMode == "0") {
                imageDisplay = "none";
            }
            else {
                imageDisplay = "inline";
            }

            $(".newsItemImage").css('display', imageDisplay);


            if (currentWindowWidth > 990) {
                maringsInPixels = currentWindowWidth - 990;

                if (columnMode == "1" || columnMode == "0")
                    numberOfColumns = 1;

                if (columnMode == "2")
                    numberOfColumns = 2;
            }
            else {
                maringsInPixels = (currentWindowWidth * 0.033) * 2;
                numberOfColumns = Math.round((currentWindowWidth - maringsInPixels) / defaultColumnWidth);
            }
        }
        else {

            maringsInPixels = (currentWindowWidth * 0.033) * 2;
            numberOfColumns = Math.round((currentWindowWidth - maringsInPixels) / defaultColumnWidth);
        }

        //numberOfColumnsInWebsitesGrid = Math.round((currentWindowWidth - maringsInPixels) / 180);

        //        if (numberOfColumns > 2)
        //            numberOfColumns = 2; ;

        var w = ($(window).width() - maringsInPixels) + "px";

        if (currentWindowWidth < 900) {
            $('.affix').css('position', 'static');
        }
        else {
            //$('.affix').css('position', 'fixed');
        }

        $('body').css('width', w);
        //$('body').css('padding', '30px');

        $("#container").css('width', w);
        $("div.container").css('width', w);
        $("#websiteContainer").css('width', w);


        ResizeTitleSizes();

        $('div.container').BlocksIt({
            numOfCol: numberOfColumns,
            offsetX: 8,
            offsetY: 8
        });

        $('#container').BlocksIt({
            numOfCol: numberOfColumns,
            offsetX: 8,
            offsetY: 8
        });

        if (currentWindowWidth < 900) {
            $(".bgscr").css('display', 'none');
        }
        else {
            $(".bgscr").css('display', 'inline');

        }

        //        $('#websiteContainer').BlocksIt({
        //            numOfCol: numberOfColumnsInWebsitesGrid,
        //            offsetX: webSitesLogosGapInPixels,
        //            offsetY: webSitesLogosGapInPixels
        //        });

        if (isImageSizeInitialized == false) {
            InitializeImageSizes();
            isImageSizeInitialized = true;
            ResizeAllMainContainersAccordingToWindowSize();
        }


    }

    function ResizeTitleSizes() {

        var min = 23;
        var max = 26;

        var titleBoxes = $("div.newsItemTitleBox");

        if (titleBoxes.length > 0) {

            if (columnMode == "1" || columnMode == "0") {
                calcFontSize = 24;
                if ($(window).width() < 500) {
                    calcFontSize = 18;
                }
                if ($(window).width() < 240) {
                    calcFontSize = 16;
                }
            }
            else
                if (columnMode == "2")
                    calcFontSize = 22;
                else
                    calcFontSize = min;

            if ($(window).width() < 500) {
                calcFontSize = 18;
            }
            if ($(window).width() < 240) {
                calcFontSize = 16;
            }

            for (var i = 0; i < titleBoxes.length; i++) {
                $(titleBoxes[i]).css('font-size', calcFontSize.toString() + "px");
            }
        }
    }

    function InitializeImageSizes() {
        var images = $("img.newsItemImage");

        //todo: image cropping by keeping negative margins is disabled by using false flag, need to reuse this block for some thing else;
        for (var i = 0; i < images.length; i++) {

            $(images[i]).attr("src", $(images[i]).attr("data-original"));

            var imgWidth = $(images[i]).attr("w");
            var imgHeight = $(images[i]).attr("h");

            if (imgWidth == 0)
                continue;

            if (imgWidth > 0) {

                var imageBox = $(images[i]).parent(".newsItemImageBox")[0];
                var imageBoxWidth = $(imageBox).width();

                imageBoxWidth = imageBoxWidth - 20;

                if (imgWidth > imageBoxWidth) {
                    imgHeight = imgHeight * (imageBoxWidth / imgWidth);
                }

                $(images[i]).css('height', imgHeight.toString() + "px");

                images[i].onload = function () {
                    $(this).css('height', "auto");

                }
            }
        }
    }

    $(document).ready(function () {

        ResizeAllMainContainersAccordingToWindowSize();

        $(window).resize();

        //blocksit define
        $(window).load(function () {
            $(window).resize();
        });

        //window resize
        var currentWidth = $(window).width();

        $(window).resize(function () {
            ResizeAllMainContainersAccordingToWindowSize();
        });
    });
